
//  SHVisitorView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/19.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit
import SnapKit

class SHVisitorView: UIView {
    
    //声明闭包
    var closure:(()->())?
    //MARK: - 懒加载控件
    //圆环
    private lazy var feedImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    //遮罩
    private lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //中间文字
    private lazy var messageLabel: UILabel = UILabel(
        text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        textColor: UIColor.grayColor(),
        font: 14,
        numberOfLines: 0,
        textAlignment: .Center)
    //登录
    private lazy var loginButton: UIButton = UIButton(
        title: "登录",
        titleColor: K_THEME_COLOR,
        font: 14,
        backgroundImgName: "common_button_white_disable",
        target: self,
        action: "didClickLogin")

    //注册
    private lazy var registerButton: UIButton = UIButton(
        title: "注册",
        titleColor: K_THEME_COLOR,
        font: 14,
        backgroundImgName: "common_button_white_disable",
        target: self,
        action: "didClickLogin")
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置非Home视图的界面 及 Home视图的动画
    func redesignVisitorView(imgName: String?, title: String?) {
        //如果参数不为nil, 重设UI
        if let _imgName = imgName, _title = title {
            iconImageView.image = UIImage(named: _imgName)
            messageLabel.text = _title
            feedImageView.hidden = true
        }else {
            //参数为nil, 不重设UI 并添加圆环动画效果
            setAnimation()
        }
        
    }
    
    private func setAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        //切换到后台或切换控制器时,不停止动画效果
        animation.removedOnCompletion = false
        //添加动画
        feedImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    //MARK: - Home界面
    private func setupUI() {
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加子控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
    }
    
    //MARK: - 监听事件
    @objc private func didClickLogin() {
        closure?()
    }
    
    //MARK: - 子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }

        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(loginButton)
            make.top.equalTo(loginButton)
            make.trailing.equalTo(messageLabel)
        }
    }
    
}
